********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global ec1 "dT1e11 dT2e11 dT3e11 dT4e11 dT5e11 dT7e11 dT8e11 dT9e11 dT10e11 dT11e11 dT12e11"
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**# Bookmark (input)
use $inputdirectory/INB_portfolio2019
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974,32)&inlist(setemi,100,704,770,173,706,708,121,120)&inlist(stanziabilita,23,24)&(monthtom>=15)&(monthtom<=375)&(monthtom!=.)&time==tm(2019m8)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2019m8) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 e11 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2019, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2019
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2019m3)&time<=tm(2020m2)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2019 12
run RO_tdintera 12 "e14 e11 exlnassets excashas"

**# Bookmark (output - column 1)
cd $outputdirectory
est clear
reghdfe sales_w $ec4 $ec1 $controls $regcommands
est save TABIIII_C1234
cd ..

*==================================================================================
global ec1 "dT1e11 dT2e11 dT3e11 dT4e11 dT5e11 dT7e11 dT8e11 dT9e11 dT10e11 dT11e11 dT12e11"
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**# Bookmark (input)
use $inputdirectory/INB_portfolio2019
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974,32)&inlist(setemi,100,704,770,173,706,708,121,120)&inlist(stanziabilita,23,24)&(monthtom>=15)&(monthtom<=375)&(monthtom!=.)&time==tm(2019m8)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==3

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2019m8) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 e11 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2019, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2019
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2019m3)&time<=tm(2020m2)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2019 12
run RO_tdintera 12 "e14 e11 exlnassets excashas"

**# Bookmark (output - column 2)
cd $outputdirectory
est clear
reghdfe sales_w $ec4 $ec1 $controls $regcommands
est save TABIIII_C1234, append
cd ..


********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global ec3 "dT1e13 dT2e13 dT3e13 dT4e13 dT5e13 dT7e13 dT8e13 dT9e13 dT10e13 dT11e13 dT12e13"
global ec5 "dT1e15 dT2e15 dT3e15 dT4e15 dT5e15 dT7e15 dT8e15 dT9e15 dT10e15 dT11e15 dT12e15"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**# Bookmark (input)
use $inputdirectory/INB_portfolio2015
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)&time==tm(2014m12)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==2

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2014m12) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 e15 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2015, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2015
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2014m7)&time<=tm(2015m6)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2015 12
run RO_tdintera 12 "e13 e15 exlnassets excashas"

**# Bookmark (output - column 3)
cd $outputdirectory
est clear
reghdfe sales_w $ec3 $ec5 $controls $regcommands
est save TABIIII_C1234, append
cd ..

*==================================================================================
global ec3 "dT1e13 dT2e13 dT3e13 dT4e13 dT5e13 dT7e13 dT8e13 dT9e13 dT10e13 dT11e13 dT12e13"
global ec5 "dT1e15 dT2e15 dT3e15 dT4e15 dT5e15 dT7e15 dT8e15 dT9e15 dT10e15 dT11e15 dT12e15"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid time#isin) vce(cluster bankid isin)"

clear *
**# Bookmark (input)
use $inputdirectory/INB_portfolio2015
gen die=0
replace die=1 if inlist(staemitit,8,9,101,94,257,67,28,29,40,86,259,92,258,105,50,55,260,276,477,479,480,574,576,577,580,587,588,591,595,799,807,812,814,816,853,910,911,913,916,921,929,940,945,950,959,974)&inlist(setemi,100,704,770)&inlist(stanziabilita,23,24)&(monthtom>=27)&(monthtom<=375)&(monthtom!=.)&time==tm(2014m12)

order bankid time cod_isin book portfolio, first
keep bankid time cod_isin book portfolio die
rename cod_isin COD_ISIN

egen dielong=max(die), by(COD_ISIN)
keep if dielong==1

keep if portfolio==3

drop portfolio
collapse (sum) book, by(bankid time COD_ISIN)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(exassets)
keep if _merge==3|_merge==2
drop _merge
replace time=tm(2014m12) if time==.
drop exassets

fillin bankid COD_ISIN time
replace book=0 if book==.
egen isin=group(COD_ISIN)
drop if isin==.
egen id=group(bankid isin)
xtset id time

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e13 e15 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_assets2015, keepus(assets)
keep if _merge!=2
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

**# Bookmark (input)
merge m:1 time COD_ISIN using $inputdirectory/INB_isinsprices2015
keep if _merge!=2
drop _merge
replace price=. if price==0
replace priceavg=. if priceavg==0

gen quantity=book/price
xtset id time

gen qsold=quantity-L.quantity
gen sales=1000000*(qsold)*priceavg/L.assets

keep if time>=tm(2014m7)&time<=tm(2015m6)
egen xxx=mean(book), by(bankid isin)
sum xxx if xxx!=0, d
drop if abs(xxx)<=10000&xxx!=0
drop xxx
winsor2 sales if sales!=0&sales!=., suffix(_w) cut(1 99)
recode sales_w (.=0)

run RO_tdummies 2015 12
run RO_tdintera 12 "e13 e15 exlnassets excashas"

**# Bookmark (output - column 4)
cd $outputdirectory
est clear
reghdfe sales_w $ec3 $ec5 $controls $regcommands
est save TABIIII_C1234, append
cd ..
